package easybooking.db;

import java.text.*;

/**
 * Abstract Class which summarizes commonly used methods for the mapper classes.
 *
 * @author Mark Roman Stehle
 */
public abstract class Mapper {

    /**
     * Converting boolean values to integer values
     *
     * @param value the boolean value which should be converted to an integer
     * @return Returns an interger value, 1 if <code>true</code> and 0 if
     * <code>false</code>.
     */
    int booleanToInt(boolean value) {
        if (value) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * Converting a date from the java util date format into a formated string.
     * The string format is corresponding to the sql date format.
     *
     * @param uDate the date in java util date format
     * @return Returns a string with a date formated in the sql date format
     * @throws ParseException
     */
    String utilDateToSqlDate(java.util.Date uDate) throws ParseException {
        DateFormat sqlDateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return sqlDateFormatter.format(uDate);
    }

    /**
     * Converting a date from the sql date format into a java util date format.
     *
     * @param sDate the date in sql date format
     * @return Returns a Date object with in java util date format
     * @throws ParseException
     */
    java.util.Date sqlDateToUtilDate(java.sql.Timestamp sDate) throws ParseException {
        DateFormat utilDateFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        return (java.util.Date) utilDateFormatter.parse(utilDateFormatter.format(sDate));
    }
}
